package com.es.rocket;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.context.ApplicationEvent;

public class RocketmqEvent extends ApplicationEvent{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static String charset = "UTF-8";
	
	private DefaultMQPushConsumer consumer;
	private List<MessageExt> msgs;
	
	

	public RocketmqEvent(Object source) {
		super(source);
		if (source instanceof Map) {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = (Map<String, Object>) source;
			@SuppressWarnings("unchecked")
			List<MessageExt> msgs =  (List<MessageExt>) map.get("msgs");
			DefaultMQPushConsumer consumer = (DefaultMQPushConsumer) map.get("consumer");
			this.consumer = consumer;
			this.msgs = msgs;
		}
	}

	public String getMsg(byte[] body){
		try {
			return new String(body, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getMsg(byte[] body, String charset){
		try {
			return new String(body, charset);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public DefaultMQPushConsumer getConsumer() {
		return consumer;
	}

	public void setConsumer(DefaultMQPushConsumer consumer) {
		this.consumer = consumer;
	}
	public List<MessageExt> getMsgs() {
		return msgs;
	}
	
	public void setMsgs(List<MessageExt> msgs) {
		this.msgs = msgs;
	}
	
}
